package isae.lib;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class InitialFunctions {

    public String monsieur(String sexe) {
        if (sexe.equals("F")) {
            return "Mademoiselle";
        } else {
            return "Monsieur";
        }
    }

    public String lettres(int n) {
        String l = "";
        Double z = n + 0.0;
        Double w = (z / 1000000 - (int) (z / 1000000)) * 1000000;
        int x = (int) (Math.rint(w * 1000.0) / 1000.0);

        String millenaire = this.lettres4(x);
        if (n == 0) {
            l = "zéro";
        } else if ((int) (n / 1000000) == 0) {
            l = this.lettres4(n);
        } else if ((int) (n / 1000000) == 1) {
            l = "un million" + this.lettres0(millenaire, " ");
        } else if ((int) (n / 1000000) > 1 && (int) (n / 1000000) <= 999) {
            l = this.lettres3((int) (n / 1000000)) + " millions" + this.lettres0(millenaire, " ");
        }
        return l;
    }

    public String lettres4(int n) {
        String centenaire = "";
        String l = "";

        Double z = n + 0.0;
        Double w = ((z / 1000) - (int) (z / 1000)) * 1000;
        int x = (int) (Math.rint(w * 1000.0) / 1000.0);

        centenaire = this.lettres3(x);
        if ((int) (n / 1000) == 0) {
            l = this.lettres3(n);
        }
        if ((int) (n / 1000) == 1) {
            l = "mille" + this.lettres0(centenaire, " ");
        }
        if ((int) (n / 1000) > 1 && (int) (n / 1000) <= 999) {
            l = this.lettres3((int) (n / 1000)) + " mille" + this.lettres0(centenaire, " ");
        }
        return l;
    }

    public String lettres3(int n) {
        String diz = "";
        String l = "";

        Double z = n + 0.0;
        Double w = ((z / 100) - (int) (z / 100)) * 100;
        int x = (int) (Math.rint(w * 1000.0) / 1000.0);

        diz = this.lettres2(x);

        if ((int) (n / 100) == 0) {
            l = this.lettres2(n);
        }
        if ((int) (n / 100) == 1) {
            l = "cent" + this.lettres0(diz, "-");
        }
        if ((int) (n / 100) > 1 && (int) (n / 100) <= 9) {
            l = this.lettres2((int) (n / 100)) + "-cent" + this.lettres0(diz, "-");
        }
        return l;
    }

    public String lettres2(int n) {
        String[] unite = new String[10];
        String[] dixvingt = new String[10];
        String[] dizaine = new String[10];
        String tempo;
        String l = "";

        unite[0] = "";
        unite[1] = "un";
        unite[2] = "deux";
        unite[3] = "trois";
        unite[4] = "quatre";
        unite[5] = "cinq";
        unite[6] = "six";
        unite[7] = "sept";
        unite[8] = "huit";
        unite[9] = "neuf";

        dixvingt[0] = "dix";
        dixvingt[1] = "onze";
        dixvingt[2] = "douze";
        dixvingt[3] = "treize";
        dixvingt[4] = "quatorze";
        dixvingt[5] = "quinze";
        dixvingt[6] = "seize";
        dixvingt[7] = "dix-sept";
        dixvingt[8] = "dix-huit";
        dixvingt[9] = "dix-neuf";

        dizaine[0] = "";
        dizaine[1] = "dix";
        dizaine[2] = "vingt";
        dizaine[3] = "trente";
        dizaine[4] = "quarante";
        dizaine[5] = "cinquante";
        dizaine[6] = "soixante";
        dizaine[7] = "soixante";
        dizaine[8] = "quatre-vingt";
        dizaine[9] = "quatre-vingt";

        if ((int) (n / 10) == 0) {
            l = unite[n];
        }
        if ((int) (n / 10) == 1) {
            System.out.println("premier if");
            Double z = n + 0.0;
            Double w = ((z / 10) - (int) (z / 10)) * 10;
            int x = (int) (Math.rint(w * 1000.0) / 1000.0);

            l = dixvingt[x];
        }
        if ((((int) (n / 10) >= 2) && ((int) (n / 10) <= 6)) || (int) (n / 10) == 8) {
            System.out.println("second if");

            Double z = n + 0.0;
            Double w = ((z / 10) - (int) (z / 10)) * 10;
            int x = (int) (Math.rint(w * 1000.0) / 1000.0);

            tempo = unite[x];
            l = dizaine[(int) (n / 10)] + this.lettres1(tempo);
        }
        if ((int) (n / 10) == 9 || (int) (n / 10) == 7) {
            System.out.println("3eme if");

            Double z = n + 0.0;
            Double w = ((z / 10) - (int) (z / 10)) * 10;
            int x = (int) (Math.rint(w * 1000.0) / 1000.0);

            tempo = dixvingt[x];
            l = dizaine[(int) (n / 10)] + this.lettres1(tempo);
        }
        System.out.println(l);
        return l;
    }

    public String lettres1(String n) {
        String l = "";
        if (n.equals("un")) {
            l = "-et-un";
        } else if (n.equals("onze")) {
            l = "-et-onze";
        } else {
            l = this.lettres0(n, "-");
        }
        return l;
    }

    public String lettres0(String n, String caracter) {
        String l = "";
        if (n.equals("") && caracter.equals("-")) {
            l = n;
        } else if (!n.equals("") && caracter.equals("-")) {
            l = "-" + n;
        } else if (n.equals("") && caracter.equals(" ")) {
            l = n;
        } else {
            l = " " + n;
        }
        return l;
    }

    public void numeroterRandom(List<isae.lib.EtudiantNoExamen> l) {
        List<isae.lib.EtudiantNoExamen> lremplie = new LinkedList<isae.lib.EtudiantNoExamen>();
        List<isae.lib.EtudiantNoExamen> lvide = new LinkedList<isae.lib.EtudiantNoExamen>();

        Random generator = new Random();
        for (isae.lib.EtudiantNoExamen e : l) {
            if (e.isRempli() == false) {
                int r = generator.nextInt();
                e.setNoexamen(r);
                lvide.add(new isae.lib.EtudiantNoExamen(e.getNoEtud(), e.getNoexamen(), e.isRempli()));
            } else {
                lremplie.add(new isae.lib.EtudiantNoExamen(e.getNoEtud(), e.getNoexamen(), e.isRempli()));
            }
        }
        Collections.sort(lremplie, new isae.lib.compareNexam());
        Collections.sort(lvide, new isae.lib.compareNexam());

        int i = 1;

        int plafond = 0;
        for (isae.lib.EtudiantNoExamen lr : lremplie) {
            plafond = lr.getNoexamen();
        }

        boolean trouver;
        for (isae.lib.EtudiantNoExamen lv : lvide) {
            trouver = false;

            while ((trouver == false) & (i <= plafond)) {
                for (isae.lib.EtudiantNoExamen lr : lremplie) {
                    if (i == lr.getNoexamen()) {
                        trouver = true;
                        break;
                    }
                    if (lr.getNoexamen() > i) {
                        break;
                    }
                }
                if (trouver == false) {
                    lv.setNoexamen(i);
                    i = i + 1;
                    break;
                } else {
                    i = i + 1;
                    trouver = false;
                }  // fin else
            }// end while

            if (i > plafond) {
                lv.setNoexamen(i);
                i = i + 1;
            }
        } //fin for lvide

        for (isae.lib.EtudiantNoExamen lv : lvide) {
            for (isae.lib.EtudiantNoExamen e : l) {
                if (lv.getNoEtud() == e.getNoEtud()) {
                    e.setNoexamen(lv.getNoexamen());
                }
            }
        }
        Collections.sort(l, new isae.lib.compareNexam());
    }

    public String getPassword(int n) {
        char[] p = new char[n];
        int c = 'A';
        int r1 = 0;
        for (int i = 0; i < n; i++) {
            r1 = (int) (Math.random() * 3);
            switch (r1) {
                case 0:
                    c = '0' + (int) (Math.random() * 10);
                    break;
                case 1:
                    c = 'a' + (int) (Math.random() * 26);
                    break;
                case 2:
                    c = 'b' + (int) (Math.random() * 26);
                    break;
            }

            p[i] = (char) c;

        }
        return new String(p);

    }

    public String gaucheZero(String str, int num) throws ClassNotFoundException {
        String s = "";
        String res = "";
        int i;
        for (i = 1; i < num - str.length() + 1; i++) {
            s = s + "0";
        }
        res = s + str;
        return res;
    }

    public String remplacer(String str) throws ClassNotFoundException {
        String strfictif = "";
        strfictif = str.replace(" ", "/");
        strfictif = strfictif.replace("'", "X");
        return strfictif;
    }

    public String nn(String chaine) {
        String ch;
        if (chaine != null) {
            ch = chaine;
            if (chaine.isEmpty()) {
                ch = "--";
            }
        } else {
            ch = "--";
        }
        return ch;
    }

    public String vide(Date chaine) {
        String ch;
        if (chaine != null) {
            ch = chaine.toString();
            if (chaine.toString().isEmpty()) {
                ch = "--";
            }
        } else {
            ch = "--";
        }
        return ch;
    }

    public String avoirMention(String note, Double noteReuss, int noteMax, int formule, String sexe) {
        String str = "";
        Double coeff = 0.0;
        int niveau = 0;
        if (formule == 1) {
            if (note.equals("Vide")) {
                niveau = 0;
            } else if (note.equals("--")) {
                niveau = 8;
            } else if (note.equals("Abs.")) {
                niveau = 7;
            } else {
                String s = String.valueOf(noteMax);
                coeff = Double.parseDouble(s) / 20;
                //  System.out.println("Coeff:" + coeff);
                //  System.out.println("noteReuss x coeff:" + noteReuss * coeff);

                Double n = Double.parseDouble(note);

                if (n < noteReuss) {
                    niveau = 1;
                } else if ((n >= noteReuss) && n < 12 * coeff) {
                    niveau = 2;
                } else if ((n >= 12 * coeff) && n < 14 * coeff) {
                    niveau = 3;
                } else if ((n >= 14 * coeff) && n < 16 * coeff) {
                    niveau = 4;
                } else if ((n >= 16 * coeff) && n < 18 * coeff) {
                    niveau = 5;
                } else if ((n >= 18 * coeff) && n <= 20 * coeff) {
                    niveau = 6;
                } else {
                    niveau = 7;
                }
            }


            switch (niveau) {
                case 0:
                    str = "Vide";
                    break;
                case 1:
                    if (sexe.equals("M")) {
                        str = "Ajourné";
                    } else {
                        str = "Ajournée";
                    }
                    break;
                case 2:
                    if (sexe.equals("M")) {
                        str = "Reçu";
                    } else {
                        str = "Reçue";
                    }
                    break;
                case 3:
                    str = "Assez Bien";
                    break;
                case 4:
                    str = "Bien";
                    break;
                case 5:
                    str = "Très Bien";
                    break;
                case 6:
                    if (sexe.equals("M")) {
                        str = "Excellent";
                    } else {
                        str = "Excellente";
                    }
                    break;
                case 8:
                    str = "--";
                    break;
                default:
                    if (sexe.equals("M")) {
                        str = "Absent";
                    } else {
                        str = "Absente";
                    }
                    break;
            }
        }
        return str;
    }

    public String avoirMention2004(String note, Double noteReuss, int noteMax, int formule, String sexe,int rachat,int rachatm) {
        String str = "";
        Double coeff = 0.0;
        int niveau = 0;
        if (formule == 1) {
            if (note.equals("Vide")) {
                niveau = 0;
            } else if (note.equals("--")) {
                niveau = 8;
            } else if (note.equals("Abs.")) {
                niveau = 7;
            } else {
                String s = String.valueOf(noteMax);
                coeff = Double.parseDouble(s) / 20;
                //  System.out.println("Coeff:" + coeff);
                //  System.out.println("noteReuss x coeff:" + noteReuss * coeff);

                Double n = Double.parseDouble(note);

                if (n < noteReuss) {
                    niveau = 1;
                } else if ((n >= noteReuss) && n < 12 * coeff) {
                    niveau = 2;
                } else if ((n >= 12 * coeff) && n < 14 * coeff) {
                    niveau = 3;
                } else if ((n >= 14 * coeff) && n < 16 * coeff) {
                    niveau = 4;
                } else if ((n >= 16 * coeff) && n < 18 * coeff) {
                    niveau = 5;
                } else if ((n >= 18 * coeff) && n <= 20 * coeff) {
                    niveau = 6;
                } else {
                    niveau = 7;
                }
            }


            switch (niveau) {
                case 0:
                    str = "Vide";
                    break;
                case 1:
                    if ((sexe.equals("M")) && (rachat==0)) {
                        str = "Ajourné";
                    } else if ((sexe.equals("F")) && (rachat==0)) {
                        str = "Ajournée";
                    } else if ((sexe.equals("M")) && (rachat==1)) {
                        str = "Reçu rachat";
                    } else if ((sexe.equals("F")) && (rachat==1)){
                        str = "Reçue rachat";
                    }
                    break;
                case 2:
                    if (sexe.equals("M") && rachatm==0) {
                        str = "Reçu";
                    } else if ((sexe.equals("F")) && (rachatm==0)) {
                        str = "Reçue";
                    } else if (rachatm==1) {
                        str = "Assez Bien";
                    }
                    break;
                case 3:
                    if (rachatm==0) {
                    str = "Assez Bien";
                    } else {
                        str="Bien";
                    }
                    break;
                case 4:
                    if (rachatm==0) {
                    str = "Bien";
                    } else {
                        str="Très Bien";
                    }
                    break;
                case 5:
                    if (rachatm==0) {
                    str = "Très Bien";
                    } else {
                        if (sexe.equals("M")) {
                            str="Excellent";
                        } else { str="Excellente";}
                    }
                    break;
                case 6:
                    if (sexe.equals("M")) {
                        str = "Excellent";
                    } else {
                        str = "Excellente";
                    }
                    break;
                case 8:
                    str = "--";
                    break;
                default:
                    if (sexe.equals("M")) {
                        str = "Absent";
                    } else {
                        str = "Absente";
                    }
                    break;
            }
        }
        return str;
    }
}